package com.learn.english.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.learn.english.model.entity.CourseVideo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author TISNIW
 * @version 1.0
 * @desc
 */
@Mapper
public interface CourseVideoMapper extends BaseMapper<CourseVideo> {
    @Select("select * from course_video where course_id = #{courseId} order by sort asc")
    List<CourseVideo> findByCourseId(Integer courseId);
    
    @Select("select * from course_video where chapter_id = #{chapterId} order by sort asc")
    List<CourseVideo> findByChapterId(Long chapterId);
    
    @Select("select * from course_video where id = #{id}")
    CourseVideo findById(Long id);
    
    @Insert("INSERT INTO course_video " +
            "(course_id, chapter_id, title, description, cover_url, video_url, sort, upload_id, status, is_free, play_count) " +
            "VALUES " +
            "(#{courseId}, #{chapterId}, #{title}, #{description}, #{coverUrl}, #{videoUrl}, #{sort}, #{uploadId}, #{status}, #{isFree}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertVideo(CourseVideo courseVideo);
    
    @Update({
            "update course_video cv",
            "join course `c` on cv.course_id = `c`.id",
            "set cv.play_count = cv.play_count + 1,",
            "    `c`.count = `c`.count + 1",
            "where cv.video_url = #{filename}"
    })
    int playVideo(String filename);
    
    @Update("update course_video set title = #{title}, description = #{description}, cover_url = #{coverUrl}, " +
            "sort = #{sort}, status = #{status}, is_free = #{isFree} where id = #{id}")
    int updateVideo(CourseVideo courseVideo);
    
    @Delete("delete from course_video where id = #{id}")
    int deleteVideo(Long id);
    
    @Delete("delete from course_video where chapter_id = #{chapterId}")
    int deleteByChapterId(Long chapterId);
    
    @Update("update course_video set sort = #{sort} where id = #{id}")
    int updateSort(Long id, Integer sort);
}
